package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.entity.Srd;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/11/6.
 */
public class SrdRecyclerViewAdapter extends RecyclerView.Adapter<SrdRecyclerViewAdapter.SrdViewHolder>{
    private ImageLoader imageLoader;
    private List<Srd.DataEntity.EntryEntity> entities;

    public SrdRecyclerViewAdapter(Context c) {
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<Srd.DataEntity.EntryEntity> datas) {
        this.entities = datas;
        notifyDataSetChanged();
    }

    @Override
    public SrdViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        LayoutInflater inflater  = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.searchresult_destination_recyclerview_item, null);
        return new SrdViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final SrdViewHolder holder, int position) {
        if (entities != null && entities.size() > 0) {
            Srd.DataEntity.EntryEntity sde = entities.get(position);
            holder.srd_CnnameTv.setText(sde.getCnname());
            holder.srd_EnnameTv.setText(sde.getEnname());
            holder.srd_TypeTv.setText(sde.getLabel());
            holder.srd_ViewsTv.setText(sde.getBeenstr());
            holder.srd_Title_Iv.setImageUrl(sde.getPhoto(), imageLoader);
        }

        if (itemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    itemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }


    private OnItemClickListener itemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getItemCount() {
        return entities != null && entities.size() > 0 ? entities.size() : 0;
    }

    public class SrdViewHolder extends RecyclerView.ViewHolder {
        private TextView srd_CnnameTv, srd_EnnameTv, srd_ViewsTv, srd_TypeTv;
        private NetworkImageView srd_Title_Iv;

        public SrdViewHolder(View v) {
            super(v);
            srd_Title_Iv = (NetworkImageView) v.findViewById(R.id.srd_Title_Iv);
            srd_CnnameTv = (TextView) v.findViewById(R.id.srd_CnnameTv);
            srd_EnnameTv = (TextView) v.findViewById(R.id.srd_EnnameTv);
            srd_ViewsTv = (TextView) v.findViewById(R.id.srd_ViewsTv);
            srd_TypeTv = (TextView) v.findViewById(R.id.srd_TypeTv);
        }
    }
}
